package com.example.gallery;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BoardHandler {
	private DBHelper helper;
	private SQLiteDatabase db;
	private static final String TAG = "BoardHandler";
	private static final String table = "BOARD";
	private static final String member_table = "MEMBER";
	
	SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	long time = System.currentTimeMillis();
	String DATE = format.format(time);
	
	private BoardHandler(Context ctx){
		this.helper = new DBHelper(ctx);
		this.db = helper.getWritableDatabase();
	}
	
	public static BoardHandler open(Context ctx) throws SQLException{
		BoardHandler handler = new BoardHandler(ctx);
		return handler;
	}
	
	public void close(){
		helper.close();
	}
	public int getNO(){
		String sql = "SELECT MAX(NO) FROM BOARD";
		int no = 0;
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor != null){
			cursor.moveToFirst();
			no = cursor.getInt(0);
		}
		return no+1;
	}
	public long insert(BoardDTO board){
		ContentValues values = new ContentValues();
		
		values.put("ID", board.getID());
		values.put("TITLE", board.getTITLE());
		values.put("CONTENT", board.getCONTENT());
		values.put("BOARD_DATE", DATE);
		
		return db.insert(table, null, values);
	
	}
	public ArrayList<BoardDTO> getList(){
		ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
		String sql = "SELECT * FROM BOARD ORDER BY NO DESC";
		
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()){
			do{
				BoardDTO board = new BoardDTO();
				board.setNO(Integer.parseInt(cursor.getString(0)));
				board.setTITLE(cursor.getString(1));
				board.setCONTENT(cursor.getString(2));
				board.setDATE(cursor.getString(3));
				board.setID(cursor.getString(4));
				list.add(board);
			}while(cursor.moveToNext());
			Log.i(TAG, "�Խñ� �� : "+cursor.getCount());
		}
		return list;
	}
	public BoardDTO getDetail(int no){
		String sql = "SELECT * FROM BOARD WHERE NO ="+no+"";
		Log.i(TAG, no+"");
		BoardDTO dto = null;

		Cursor cursor = db.rawQuery(sql, null);
		if(cursor != null){
			cursor.moveToFirst();
				dto = new BoardDTO();
				dto.setNO(Integer.parseInt(cursor.getString(0)));
				dto.setTITLE(cursor.getString(1));
				dto.setCONTENT(cursor.getString(2));
				dto.setDATE(cursor.getString(3));
				dto.setID(cursor.getString(4));
		}
		
		return dto;
	}
	public int update(BoardDTO dto){
		ContentValues values = new ContentValues();
		values.put("TITLE", dto.getTITLE());
		values.put("CONTENT", dto.getCONTENT());
		values.put("BOARD_DATE", DATE);
		
/*		db.execSQL("UPDATE BOARD SET " +
								"TITLE = "+dto.getTITLE()+
								", CONTENT = "+dto.getCONTENT()+
								", BOARD_DATE ="+DATE +
							"WHERE NO = "+dto.getNO());
*/		return db.update(table, values, "NO = ?", new String[] {dto.getNO()+""} );
	}
	public int delete(int no){
		return 	db.delete(table, "NO = ?", new String[] {no+""});
	}
	

	
}
